package com.hs.carrental.controller;

import com.hs.carrental.common.PicUploadResult;
import com.hs.carrental.common.Result;
import com.hs.carrental.service.FileUploadService;
import com.hs.carrental.vo.ImageVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fileUpload")
@Slf4j
public class FileUploadController {

    // 允许上传的格式 图片形式
    private static final String[] IMAGE_TYPE = new String[]{".bmp", ".jpg", ".jpeg", ".png"};

    @Resource
    private FileUploadService fileUploadService;

    /**
     * 单图片上传
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/uploadImg")
    public Result uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        boolean isFlag = false;
        for (String type : IMAGE_TYPE) {
            log.info("图片名称:{}",file.getOriginalFilename());
            if (StringUtils.endsWithIgnoreCase(file.getOriginalFilename(), type)) {
                isFlag = true;
                break;
            }
        }
        if (isFlag) {
            PicUploadResult picUploadResult = fileUploadService.uploadImg(file, request);
            boolean isLegal = picUploadResult.isLegal();

            if (isLegal) {
                Map resMap = new HashMap<>();
                resMap.put("imgPath", picUploadResult.getImgPath());
                return Result.ok(resMap);
            } else {
                return Result.failed("图片上传有误");
            }
        } else {
            return Result.failed("上传的图片格式必须为:bmp,jpg,jpeg,png");
        }
    }

    /**
     * 多图片上传
     * @param files
     * @param request
     * @return
     */
    @PostMapping("/uploadImages")
    public Result uploadImages(@RequestParam("files") MultipartFile[] files, HttpServletRequest request) {
        //定义一个标识
        boolean isFlag = false;
        //遍历
        for (MultipartFile uploadFile : files) {
            for (String type : IMAGE_TYPE) {
                //如果文件以{".bmp", ".jpg", ".jpeg", ".png"}中任何一个结尾的话就跳出循环
                if (StringUtils.endsWithIgnoreCase(uploadFile.getOriginalFilename(), type)) {
                    isFlag = true;
                    break;
                }
            }
        }
        //如果为true
        if (isFlag) {
            //上传图片的逻辑处理
            PicUploadResult picUploadResult = fileUploadService.uploadManyImg(files, request);
            boolean isLegal = picUploadResult.isLegal();
            //如果服务层处理没有问题
            if (isLegal) {
                Map resMap = new HashMap<>();
                //定义需要返回给前端的数据格式
                List<ImageVo> imageVos = new ArrayList<>();
                //获取文件的保存路径 或许有多个
                List<String> imgPaths = picUploadResult.getImgPaths();
                String name = "";
                ImageVo imageVo = null;
                //遍历文件保存路径 封装需要返回给前端的数据
                for (int i = 0; i < imgPaths.size(); i++) {
                    imageVo = new ImageVo();
                    //假设url为http://localhost:9527/images/111.jpg 那么split就应该为["http:","","localhost:9527","images","111.jpg"]
                    String url = imgPaths.get(i);
                    String[] split = url.split("/");
                    //所以文件名为数组最后一个元素
                    name = split[split.length-1];
                    imageVo.setName(name);
                    imageVo.setUrl(url);
                    //添加到集合中去
                    imageVos.add(imageVo);
                }
                //封装完成 返回给前端
                resMap.put("images", imageVos);
                return Result.ok(resMap);
            } else {
                return Result.failed("图片上传有误");
            }
        } else {
            //如果isFlag为false 返回失败
            return Result.failed("上传的图片格式必须为:bmp,jpg,jpeg,png");
        }
    }
}
